package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/constant"
	"shop-bean/model"
	"shop-bean/request"
	"shop-common/library/serializer"
	"shop-common/utils"
	"shop-common/utils/contextArgs"
	"shop-common/utils/queryWrapper"
	"shop-service/business"
)

type AddrController struct {
}

const StaticUserId = "165161616166"

// @Tags 地址接口
// @description: 新增用户地址
// @Summary 新增用户地址
// @Security ApiKeyAuth
// @function: Save
// @Accept  json
// @Produce  json
// @Param data body request.AddrRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /address/addAddr [post]
func (c *AddrController) Save(ctx *gin.Context) {
	form := request.AddrRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if form.AddrId > 0 {
		serializer.Fail(ctx, "该地址已存在")
		return
	}

	/*userId, err := contextArgs.GetUserUUID(ctx)
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}*/

	count, err := business.ServiceImpl().UserAddrService.Count(StaticUserId)
	entity := &model.UserAddr{}
	if count == 0 {
		entity.CommonAddr = constant.GLOBAL_ENABLE
	} else {
		entity.CommonAddr = constant.GLOBAL_DISABLE
	}
	entity.UserId = StaticUserId
	entity.Status = constant.GLOBAL_ENABLE
	utils.StructMap(form, entity)

	err = business.ServiceImpl().UserAddrService.Create(entity)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicCreateErr)
		return
	}
	serializer.Success(ctx, "添加地址成功")
}

// @Tags 地址接口
// @Summary 根据地址id，获取地址信息
// @Security ApiKeyAuth
// @function: Info
// @Accept  json
// @Produce  json
// @Param imgId path string true "主键"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /address/addrInfo/{addrId} [get]
func (c *AddrController) Info(ctx *gin.Context) {
	addrId, err := contextArgs.GetUriIntPath(ctx, "addrId")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	userAddr, err := business.ServiceImpl().UserAddrService.FindByUserId(uint(addrId), StaticUserId)
	if userAddr == nil {
		serializer.Fail(ctx, "该地址已被删除")
		return
	}

	serializer.SuccessData(ctx, "", userAddr)
}

// @Tags 地址接口
// @description: 修改订单用户地址
// @Summary 修改用户地址
// @Security ApiKeyAuth
// @function: Update
// @Accept  json
// @Produce  json
// @Param data body request.AddrRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /address/updateAddr [put]
func (c *AddrController) Update(ctx *gin.Context) {
	form := request.AddrRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}
	userId, err := contextArgs.GetUserUserId(ctx)
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	dbUserAddr, err := business.ServiceImpl().UserAddrService.FindByUserId(1, userId)
	if dbUserAddr == nil {
		serializer.Fail(ctx, "该地址已被删除")
		return
	}

	entity := &model.UserAddr{}
	utils.StructMap(form, entity)
	entity.UserId = userId
	// 更新sql
	// 清除当前地址缓存
	// 清除默认地址缓存
	serializer.Success(ctx, "")
}

// @Tags 地址接口
// @description: 设置默认地址
// @Summary 根据地址id，设置默认地址
// @Security ApiKeyAuth
// @function: Default
// @Accept  json
// @Produce  json
// @Param addrId path string true "主键ID"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /address/defaultAddr/{addrId} [get]
func (c *AddrController) Default(ctx *gin.Context) {
	addrId, err := contextArgs.GetUriIntPath(ctx, "addrId")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	err = business.ServiceImpl().UserAddrService.UpdateDefaultUserAddr(StaticUserId, uint(addrId))
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 地址接口
// @description: 设置默认地址
// @Summary 根据地址id，设置默认地址
// @Security ApiKeyAuth
// @function: List
// @Accept  json
// @Produce  json
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /address/list [get]
func (c *AddrController) List(ctx *gin.Context) {
	pair := queryWrapper.Eq("user_id", StaticUserId)
	list, err := business.ServiceImpl().UserAddrService.Find(pair)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", list)
}

// @Tags 地址接口
// @description: 设置默认地址
// @Summary 根据地址id，设置默认地址
// @Security ApiKeyAuth
// @function: Delete
// @Accept  json
// @Produce  json
// @Param addrId path string true "主键ID"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /address//deleteAddr/{addrId} [delete]
func (c *AddrController) Delete(ctx *gin.Context) {
	addrId, err := contextArgs.GetUriIntPath(ctx, "addrId")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	serializer.SuccessData(ctx, "", addrId)
}
